package JavaClass.demo01;

import java.util.Arrays;

public class Practice01 {
    public static void exercise1(){
        String str = "张三丰-19-男-哈尔滨市南岗区东方新天地";

        String[] fields = str.split("-");
        System.out.println("姓名:"+fields[0]);
        System.out.println("年龄:"+fields[1]);
        System.out.println("性别:"+fields[2]);
        System.out.println("地址:"+fields[3]);
    }

    public static void exercise2(String string){
        StringBuffer sb = new StringBuffer(string);
        sb.reverse();

        if(string.equals(sb.toString())){
            System.out.println("是回文");
        }else {
            System.out.println("不是回文");
        }
    }

    public static void exercise3(){
        String str="this is a test of java";
        //1.使用更简单的方法，replace()
        int oldLength=str.length();
        int newLength=str.replaceAll("s", "").length();

        System.out.println("s个数量"+(oldLength-newLength));

        //2
        System.out.println(str.substring(10,14));

        //3;
        char[] cstr=str.toCharArray();
        System.out.println(Arrays.toString(cstr));

        char[] ccstr=new char[str.length()];

        for(int i=0;i<str.length();i++) {
            ccstr[i]=str.charAt(i);
        }

        System.out.println(Arrays.toString(ccstr));

        //(4) 将字符串中每个单词的第一个字母变成大写， 输出到控制台。

        StringBuffer sb=new StringBuffer(str);

        sb.replace(0,1,sb.substring(0, 1).toUpperCase());
        for(int i=1;i<sb.length();i++) {
            if(sb.substring(i-1, i).equals(" ")) {
                sb.replace(i,i+1,sb.substring(i, i+1).toUpperCase());
            }
        }

        System.out.println(sb.toString());

        //(5) 用两种方式实现该字符串的倒叙输出。(用StringBuffer和for循环方式分别实现)
        System.out.println(new StringBuffer(str).reverse());


        for(int i=str.length()-1;i>=0;i--) {
            System.out.print(str.charAt(i));
        }

        System.out.println();

        //(6) 将本字符串转换成一个字符串数组，要求每个数组元素都是一个有意义的额英文单词,并输出到控制台

        String[] a=str.split(" ");

        System.out.println(Arrays.toString(a));

    }


    public static void main(String[] args) {
        Practice01.exercise1();
        Practice01.exercise2("2002");
        Practice01.exercise3();
    }
}
